package com.ydkfjs.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ydkfjs.model.dto.ItemRequest;
import com.ydkfjs.model.po.Item;
import com.ydkfjs.model.vo.ItemVO;
import com.ydkfjs.service.ItemService;
import com.ydkfjs.mapper.ItemMapper;
import com.ydkfjs.utils.HBAssert;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author hb
 * @description 针对表【t_item】的数据库操作Service实现
 * @createDate 2024-04-02 10:34:56
 */
@Service
public class ItemServiceImpl extends ServiceImpl<ItemMapper, Item>
        implements ItemService {

    private final ItemMapper itemMapper;

    public ItemServiceImpl(ItemMapper itemMapper) {
        this.itemMapper = itemMapper;
    }

    @Override
    public void add(ItemRequest itemRequest) {
        // DB类型转换
        Item item = BeanUtil.copyProperties(itemRequest, Item.class);

        // 存入数据库
        int insert = itemMapper.insert(item);

        // 判断操作是否正确
        HBAssert.isTrue(insert > 0, "新增失败");
    }

    @Override
    public List<ItemVO> getList(Integer kind, Integer type) {

        // 根据分配获取物品列表
        List<Item> items = itemMapper.selectList(new LambdaQueryWrapper<Item>()
                .eq(Item::getTypeId, type)
                .eq(Item::getKind, kind));

        // VO类转换
        List<ItemVO> itemVOS = BeanUtil.copyToList(items, ItemVO.class);
        return itemVOS;
    }

    @Override
    public void deleteById(Long id) {
        // 根据id删除
        int i = itemMapper.deleteById(id);

        // 判断操作是否正确
        HBAssert.isTrue(i > 0,"删除失败");
    }
}




